package com.suning.sawp.po.commodity;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@XStreamAlias("MbfBody")
@XStreamConverter(ReceiveBppGoodsInfo.class)
public class ReceiveBppGoodsInfo extends ConverterAdaptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReceiveBppGoodsInfo.class);

    private List<BppMqCommodity> data;

    public List<BppMqCommodity> getData() {
        return this.data;
    }

    public void setData(List<BppMqCommodity> data) {
        this.data = data;
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        LOGGER.info("ReceiveBppGoodsInfo,{}", reader.toString());
        ReceiveBppGoodsInfo receiveBppGoodsInfo = new ReceiveBppGoodsInfo();
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            boolean isNeedChannel = false;
            if ("commList".equals(reader.getNodeName())) {
                List<BppMqCommodity> commodities = new ArrayList<BppMqCommodity>();
                while (reader.hasMoreChildren()) {
                    reader.moveDown();
                    if ("MpsSearchInfoBo".equals(reader.getNodeName())) {
                        BppMqCommodity commodity = new BppMqCommodity();
                        while (reader.hasMoreChildren()) {
                            reader.moveDown();
                            if ("chanList".equals(reader.getNodeName())) {
                                while (reader.hasMoreChildren()) {
                                    if (isNeedChannel) {
                                        break;
                                    }
                                    reader.moveDown();
                                    if ("chanInfo".equals(reader.getNodeName())) {
                                        isNeedChannel = false;
                                        while (reader.hasMoreChildren()) {
                                            reader.moveDown();
                                            if (("chanCode".equals(reader.getNodeName()))
                                                    && ("CODE".equals(reader.getValue()))) {
                                                isNeedChannel = true;
                                                reader.moveUp();
                                                break;
                                            }
                                            reader.moveUp();
                                        }
                                    }
                                    reader.moveUp();
                                }
                            }
                            if ("activityId".equals(reader.getNodeName())) {
                                commodity.setActivityId(reader.getValue());
                            }
                            if ("serviceType".equals(reader.getNodeName())) {
                                commodity.setServiceType(reader.getValue());
                            }
                            if ("commCode".equals(reader.getNodeName())) {
                                commodity.setCommCode(reader.getValue());
                            }
                            if ("partName".equals(reader.getNodeName())) {
                                commodity.setPartName(reader.getValue());
                            }
                            if ("mertCode".equals(reader.getNodeName())) {
                                commodity.setMertCode(reader.getValue());
                            }
                            if ("gbPrice".equals(reader.getNodeName())) {
                                commodity.setGbPrice(reader.getValue());
                            }
                            if ("payPrice".equals(reader.getNodeName())) {
                                commodity.setPayPrice(reader.getValue());
                            }
                            if ("published".equals(reader.getNodeName())) {
                                commodity.setPublished(reader.getValue());
                            }
                            if ("categCode".equals(reader.getNodeName())) {
                                commodity.setCategCode(reader.getValue());
                            }
                            if ("gbBeginDate".equals(reader.getNodeName())) {
                                try {
                                    commodity.setGbBeginDate(
                                            DateUtils.parseDate(reader.getValue(), "yyyy-MM-dd HH:mm:ss"));
                                } catch (ParseException e) {
                                    LOGGER.error("commodity BeginDate parse error", e);
                                }
                            }
                            if ("gbEndDate".equals(reader.getNodeName())) {
                                try {
                                    commodity.setGbEndDate(
                                            DateUtils.parseDate(reader.getValue(), "yyyy-MM-dd HH:mm:ss"));
                                } catch (ParseException e) {
                                    LOGGER.error("commodity EndDate parse error", e);
                                }
                            }
                            reader.moveUp();
                        }
                        if (isNeedChannel) {
                            commodities.add(commodity);
                        }
                    }
                    reader.moveUp();
                }
                receiveBppGoodsInfo.setData(commodities);
            }
            reader.moveUp();
        }
        return receiveBppGoodsInfo;
    }
}
